#!/bin/sh

disable_wifi_ap_mode() {
	# ap_onboot设置为0，表示wlan不再启动AP模式，onboarding也不会再启动
	ubus call wlan_manager wlan_sta_set_init_status '{"ap_onboot":0}'
}

disable_wifi() {
	# 不再卸载驱动，配合写码工具更新DEVICE ID
	ubus call wlan_manager wlan_ap_stop '{"reload_driver":0}'
	ubus call wlan_manager wlan_sta_stop '{"reload_driver":0}'
}

enable_wifi() {
	local ap_onboot=`uci get wlan.ap0.on_boot`
	local sta_onboot=`uci get wlan.sta0.on_boot`
	local factory_mode="`uci get tp_manage.factory_mode.enabled`"

	# ap_onboot uci is "off" means onboarding already successed, do not open AP again
	if [ "off" != "$ap_onboot" -o "1" == "$factory_mode" ]; then
		ubus call wlan_manager wlan_ap_start
	fi
	# sta_onboot uci is "off" means do not want to start sta, do not open STA again
	if [ "off" != "$sta_onboot" -o "1" == "$factory_mode" ]; then
		ubus call wlan_manager wlan_sta_start
	fi
}

switch_to_wifi() {
	enable_wifi
	uci set network.wan.ifname=wlan0
	uci set protocol.wan.ifname=wlan0
	uci set protocol.static.ifname=wlan0
	uci set protocol.dhcp.ifname=wlan0
	uci set protocol.pppoe.ifname=wlan0
	uci set network.ban.ifname=eth0
}

switch_to_ethernet() {
	disable_wifi
	uci set network.wan.ifname=eth0
	uci set protocol.wan.ifname=eth0
	uci set protocol.static.ifname=eth0
	uci set protocol.dhcp.ifname=eth0
	uci set protocol.pppoe.ifname=eth0
	uci set network.ban.ifname=wlan0
}

switch_network_mode() {
	if [ "up" = $1 ]; then
		echo "switch to ethernet..." >> /dev/console
		switch_to_ethernet
	elif [ "down" = $1 ]; then
		echo "switch to wifi..." >> /dev/console
		switch_to_wifi
	fi

	track-reload network

	echo "switch done" >> /dev/console
}

start_dhcp_client() {
	local wan_proto=`uci get network.wan.proto`
	local dhcpc_enable=`uci get dhcpc.udhcpc.enable`
	local fac_mode=`uci get tp_manage.factory_mode.enabled`

	if [ "dhcp" != "${wan_proto}" -a "1" != "${fac_mode}" ]; then

		if [ "0" = "${dhcpc_enable}" ]; then
			uci set dhcpc.udhcpc.enable=1
			uci commit dhcpc
		fi

		/lib/netifd/static_dhcp_start.sh
	fi
}

check_network_mode () {
	first_for_wifi=1

	while [ "1" = "1" ]
	do
		curr_wan_dev=`uci get network.wan.ifname`
		eth_link_status=`ifconfig eth0 | grep RUNNING`

		if [ "" != "${eth_link_status}" ]; then
			if [ 1 = ${first_for_wifi} ]; then
				echo "first time to disable wifi" >> /dev/console
				ubus send eth_event '{"eth_state":"up"}'
				disable_wifi_ap_mode
				first_for_wifi=0
				disable_wifi
				start_dhcp_client
				ubus call cloudclient construct_ssl_path '{"service_type":1, "source": "emergency_module"}'
			fi
			if [ "eth0" != ${curr_wan_dev} ]; then
				echo "eth0 change to up, need switch network mode" >> /dev/console
				ubus send eth_event '{"eth_state":"up"}'
				disable_wifi_ap_mode
				switch_network_mode "up"
				start_dhcp_client
			fi
		else
			if [ "eth0" = ${curr_wan_dev} ]; then
				echo "eth0 change to down, need switch network mode" >> /dev/console
				ubus send eth_event '{"eth_state":"down"}'
				switch_network_mode "down"
			fi
		fi

		sleep 2
	done
}

check_network_mode
